package com.wjw.service;

import com.wjw.mapper.RatingJudgeTableMapper;
import com.wjw.model.Rating_judge_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Rating_judge_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RatingJudgeTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);

    public List<Rating_judge_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);
        try {
            List<Rating_judge_table> all = mapper.findAll();
            List<Rating_judge_table_view> a=new ArrayList<Rating_judge_table_view>();
            for(Rating_judge_table e:all){
                Rating_judge_table_view d = new Rating_judge_table_view();
                d.setEvaluate_grade_name(mapper.findGradeById(e.getEvaluate_grade_id()).getEvaluate_grade_name());
                d.setEvaluate_index_name(mapper.findIndexById(e.getEvaluate_index_id()).getEvaluate_index_name());
                d.setRoadway_type_name(mapper.findRoadwayById(e.getRoadway_type_id()).getRoadway_type_name());
                d.setRating_judge_max(e.getRating_judge_max());
                d.setRating_judge_min(e.getRating_judge_min());
                d.setRating_judge_id(e.getRating_judge_id());
                a.add(d);
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Rating_judge_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Rating_judge_table> all = mapper.findByPage(start,limit);
            List<Rating_judge_table_view> a=new ArrayList<Rating_judge_table_view>();
            for(Rating_judge_table e:all){
                Rating_judge_table_view d = new Rating_judge_table_view();
                d.setEvaluate_grade_name(mapper.findGradeById(e.getEvaluate_grade_id()).getEvaluate_grade_name());
                d.setEvaluate_index_name(mapper.findIndexById(e.getEvaluate_index_id()).getEvaluate_index_name());
                d.setRoadway_type_name(mapper.findRoadwayById(e.getRoadway_type_id()).getRoadway_type_name());
                d.setRating_judge_max(e.getRating_judge_max());
                d.setRating_judge_min(e.getRating_judge_min());
                d.setRating_judge_id(e.getRating_judge_id());
                a.add(d);
            }
            return a;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Rating_judge_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);
        try {
            Rating_judge_table d = new Rating_judge_table();
            d.setRoadway_type_id(mapper.findRoadwayByName(e.getRoadway_type_name()).getRoadway_type_id());
            d.setEvaluate_grade_id(mapper.findGradeByName(e.getEvaluate_grade_name()).getEvaluate_grade_id());
            d.setEvaluate_index_id(mapper.findIndexByName(e.getEvaluate_index_name()).getEvaluate_index_id());
            d.setRating_judge_max(e.getRating_judge_max());
            d.setRating_judge_min(e.getRating_judge_min());
            d.setRating_judge_id(e.getRating_judge_id());
            mapper.add(d);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Rating_judge_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RatingJudgeTableMapper mapper=sqlsession.getMapper(RatingJudgeTableMapper.class);
        try {
            Rating_judge_table d = new Rating_judge_table();
            d.setRoadway_type_id(mapper.findRoadwayByName(e.getRoadway_type_name()).getRoadway_type_id());
            d.setEvaluate_grade_id(mapper.findGradeByName(e.getEvaluate_grade_name()).getEvaluate_grade_id());
            d.setEvaluate_index_id(mapper.findIndexByName(e.getEvaluate_index_name()).getEvaluate_index_id());
            d.setRating_judge_max(e.getRating_judge_max());
            d.setRating_judge_min(e.getRating_judge_min());
            d.setRating_judge_id(e.getRating_judge_id());
            mapper.change(d);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
